#include <iostream>
#include <io.h>
#include <fstream>
#include <string>
#include <vector>
using namespace std;

// Input： 目录名称
// Output：文件列表清单
class Findupdate {
public:
    void replaceFileNameList();//遍历文件列表替换文件名
    bool checkFile(string path);//查找路径下所有文件，不存在子文件夹
    bool checkFile_circ(string path, vector<string>& m_vec_file_path_name);//查找路径下所有文件，存在子文件夹

    ~Findupdate()
    {
        m_vec_file_path_name.clear();
    }
    vector<string> m_vec_file_path_name; //文件名路径列表
private:
    bool replaceFileName(string oldFileName, string newFileName);//替换文件名 
    string m_modif_before = ".stp";             //指定修改前的字段
};

bool Findupdate::replaceFileName(string oldFileName, string newFileName)
{
    fstream fs;
    fs.open(oldFileName.c_str());
    if (fs.fail())
    {
        cout << "文件打开失败！" << endl;
        fs.close();
        return false;
    }
    else
    {
        fs.close();
        if (rename(oldFileName.c_str(), newFileName.c_str()) == -1)   //文件重命名
        {
            cout << "文件名修改失败！" << endl;
            return false;
        }
        return true;
    }
    return true;
}

void Findupdate::replaceFileNameList()
{
    int num = 1;
    string ModifyAfterFilds = "";                  //指定修改后的字段
    for (auto name : m_vec_file_path_name)
    {
        //临时文件名用于字符替换
        string newFileName = name;
        int findIndex = name.find(m_modif_before, 1);
        if (findIndex != -1 && findIndex == (name.size()-4))
        {
            ModifyAfterFilds = ".stp-0" + std::to_string(num); 
            num++;
            newFileName = newFileName.replace(findIndex, m_modif_before.size(), ModifyAfterFilds);
            cout << name << endl;
            cout << newFileName << endl;
        }
        else
        {
            continue;
        }        
        int ret = replaceFileName(name, newFileName);
    }
}

bool Findupdate::checkFile(string path)
{
	string EachPath = path;      //临时路径，用于字符拼接
	intptr_t FileHandle;             //文件句柄
	struct _finddata_t FileInfo;     //文件信息

	if ((FileHandle = _findfirst(EachPath.append("\\*").c_str(), &FileInfo)) == -1)
	{
		cout << "未找到文件! " << endl;
		return false;
	}
	else
	{
		while (_findnext(FileHandle, &FileInfo) == 0)
		{
			if (strcmp(FileInfo.name, ".") != 0 && strcmp(FileInfo.name, "..") != 0)
			{
				m_vec_file_path_name.push_back(FileInfo.name);
				cout << "FileName : " << FileInfo.name << endl;
			}
		}
		_findclose(FileHandle);
	}
	return true;
}

bool Findupdate::checkFile_circ(string path, vector<string>& m_vec_file_path_name)
{
	string EachPath = path;      //临时路径，用于字符拼接
	intptr_t FileHandle;             //文件句柄
	struct _finddata_t FileInfo;     //文件信息

	//使用_findfirst查找文件，获取文件信息
	if ((FileHandle = _findfirst(EachPath.append("\\*").c_str(), &FileInfo)) == -1)
	{
		cout << "failed find file! " << endl;
		return false;
	}
	else
	{
		do
		{   // 比较文件类型是否是文件夹
			if ((FileInfo.attrib &  _A_SUBDIR))
			{
				if (strcmp(FileInfo.name, ".") != 0 && strcmp(FileInfo.name, "..") != 0)
				{
					EachPath = path;   //每次从根路径拼接
					//递归调用
					checkFile_circ(EachPath.append("\\").append(FileInfo.name), m_vec_file_path_name);
				}
			}
			else
			{
				EachPath = path;
				m_vec_file_path_name.push_back(EachPath.append("\\").append(FileInfo.name));

				cout << "PATH/FileName : " << EachPath << endl;
			}
		} while (_findnext(FileHandle, &FileInfo) == 0);
		_findclose(FileHandle);
	}
	return true;
}

int main()
{
	string Path = "E:\\git_warehouse\\data_treating\\file_rename";      //自定义路径
    Findupdate f_upate;
	
  //  f_upate.checkFile(Path);
    f_upate.checkFile_circ(Path, f_upate.m_vec_file_path_name);
    f_upate.replaceFileNameList();

	system("pause");
	return 0;
}
